package com.yoku.view.event;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * Created by xiahongjie on 2017/3/11.
 */
@Slf4j
@Component
public class TestListener {

    @Autowired
    private EventBus eventBus;

    ExecutorService executorService;

    @PostConstruct
    public void init(){
        eventBus.register(this);
        executorService = Executors.newFixedThreadPool(5);
    }

    @Subscribe
    public void onListener(TestEvent testEvent){
        executorService.submit(new Mytask(testEvent));
        log.info(Thread.currentThread().getName() + testEvent.getName());
    }

    class Mytask implements Runnable{

        TestEvent testEvent;

        @Override
        public void run() {
            try {
                Thread.sleep(3000L);
                log.info(Thread.currentThread().getName() + testEvent.getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public Mytask(TestEvent testEvent){
            this.testEvent = testEvent;
        }
    }

}
